﻿#include "dingdan.h"
#include "tools.h"
#include <QJsonObject>
#include "dbhelper.h"

Dingdan::Dingdan(QObject *parent) : QObject(parent)
{
    state = Dingdan::SAVED;
    isTicket = 0;
    sourceFreightPaid = 0;
    sourceFreightUnpaid = 0;
    targetFreightPaid = 0;
    ticket = 0;
    money = 0;
    freeTicket = 0;
    freeMoney = 0;
    totalCount = 0;
    totalMoney = 0;
    ticketIn = 0;
    ticketOut = 0;
    moneyIn = 0;
    moneyOut = 0;
    profit = 0;

    this->relation = nullptr;
    this->reverseRelation = nullptr;
}

Dingdan::Dingdan(QJsonObject dingdanObj){
    Dingdan();
    loadFromJsonObject(dingdanObj);
}

void Dingdan::loadFromJsonObject(QJsonObject obj){
    id = obj.value("id").toInt();
    dingdanId = obj.value("dingdan_id").toString();
    relatedDingdanId = obj.value("related_dingdan_id").toString();
    sourceUsername = obj.value("source_username").toString();
    sourceCompanyId = obj.value("source_company_id").toString();
    targetUsername = obj.value("target_username").toString();
    targetCompanyId = obj.value("target_company_id").toString();
    state = obj.value("state").toInt();
    type = obj.value("type").toInt();
    date = obj.value("date").toString();

    isTicket = obj.value("is_ticket").toInt();
    sourceFreightPaid = obj.value("source_freight_paid").toDouble();
    sourceFreightUnpaid = obj.value("source_freight_unpaid").toDouble();
    targetFreightPaid = obj.value("target_freight_paid").toDouble();
    ticket = obj.value("ticket").toDouble();
    money = obj.value("money").toDouble();
    freeTicket = obj.value("free_ticket").toDouble();
    freeMoney = obj.value("free_money").toDouble();

    totalCount = obj.value("total_count").toInt();
    totalMoney = obj.value("total_money").toDouble();

    ticketIn = obj.value("ticket_in").toDouble();
    ticketOut = obj.value("ticket_out").toDouble();
    moneyIn = obj.value("money_in").toDouble();
    moneyOut = obj.value("money_out").toDouble();
    profit = obj.value("profit").toDouble();
    remark = obj.value("remark").toString();
    opNickName = obj.value("op_nick_name").toString();

//    Tools::cout("id:"+QString("%1").arg(id));
//    Tools::cout("dingdanId:"+dingdanId);
//    Tools::cout("sourceUsername:"+sourceUsername);
//    Tools::cout("sourceCompanyId:"+sourceCompanyId);
//    Tools::cout("targetUsername:"+targetUsername);
//    Tools::cout("targetCompanyId:"+targetCompanyId);
//    Tools::cout("state:"+QString("%1").arg(state));
//    Tools::cout("type:"+QString("%1").arg(type));
//    Tools::cout("date:"+date);
//    Tools::cout("isTicket:"+QString("%1").arg(isTicket));
//    Tools::cout("sourceFreightPaid:"+QString("%1").arg(sourceFreightPaid));
//    Tools::cout("sourceFreightUnpaid:"+QString("%1").arg(sourceFreightUnpaid));
//    Tools::cout("targetFreightPaid:"+QString("%1").arg(targetFreightPaid));
//    Tools::cout("ticket:"+QString("%1").arg(ticket));
//    Tools::cout("money:"+QString("%1").arg(money));
//    Tools::cout("freeTicket:"+QString("%1").arg(freeTicket));
//    Tools::cout("freeMoney:"+QString("%1").arg(freeMoney));
//    Tools::cout("totalCount:"+QString("%1").arg(totalCount));
//    Tools::cout("totalMoney:"+QString("%1").arg(totalMoney));
//    Tools::cout("ticketIn:"+QString("%1").arg(ticketIn));
//    Tools::cout("ticketOut:"+QString("%1").arg(ticketOut));
//    Tools::cout("moneyIn:"+QString("%1").arg(moneyIn));
//    Tools::cout("moneyOut:"+QString("%1").arg(moneyOut));
//    Tools::cout("profit:"+QString("%1").arg(profit));
//    Tools::cout("remark:"+remark);
//    Tools::cout("opNickName:"+opNickName);
}

QString Dingdan::toJson(){
    QString jsonStr = "{";

    jsonStr.append("\"dingdan_id\":"+Tools::toJsonStr(dingdanId)+",");
    jsonStr.append("\"related_dingdan_id\":"+Tools::toJsonStr(relatedDingdanId)+",");
    jsonStr.append("\"source_username\":"+Tools::toJsonStr(sourceUsername)+",");
    jsonStr.append("\"source_company_id\":"+Tools::toJsonStr(sourceCompanyId)+",");
    jsonStr.append("\"target_username\":"+Tools::toJsonStr(targetUsername)+",");
    jsonStr.append("\"target_company_id\":"+Tools::toJsonStr(targetCompanyId)+",");
    jsonStr.append("\"state\":"+QString("%1").arg(state)+",");
    jsonStr.append("\"type\":"+QString("%1").arg(type)+",");
    jsonStr.append("\"date\":"+Tools::toJsonStr(date)+",");
    jsonStr.append("\"is_ticket\":"+QString("%1").arg(isTicket)+",");
    jsonStr.append("\"source_freight_paid\":"+QString("%1").arg(sourceFreightPaid)+",");
    jsonStr.append("\"source_freight_unpaid\":"+QString("%1").arg(sourceFreightUnpaid)+",");
    jsonStr.append("\"target_freight_paid\":"+QString("%1").arg(targetFreightPaid)+",");
    jsonStr.append("\"ticket\":"+QString("%1").arg(ticket)+",");
    jsonStr.append("\"money\":"+QString("%1").arg(money)+",");
    jsonStr.append("\"free_ticket\":"+QString("%1").arg(freeTicket)+",");
    jsonStr.append("\"free_money\":"+QString("%1").arg(freeMoney)+",");
    jsonStr.append("\"total_count\":"+QString("%1").arg(totalCount)+",");
    jsonStr.append("\"total_money\":"+QString("%1").arg(totalMoney)+",");
    jsonStr.append("\"ticket_in\":"+QString("%1").arg(ticketIn)+",");
    jsonStr.append("\"ticket_out\":"+QString("%1").arg(ticketOut)+",");
    jsonStr.append("\"money_in\":"+QString("%1").arg(moneyIn)+",");
    jsonStr.append("\"money_out\":"+QString("%1").arg(moneyOut)+",");
    jsonStr.append("\"profit\":"+QString("%1").arg(profit)+",");
    jsonStr.append("\"remark\":"+Tools::toJsonStr(remark)+",");
    jsonStr.append("\"op_nick_name\":"+Tools::toJsonStr(opNickName));

    jsonStr.append("}");

    return jsonStr;
}

QString Dingdan::getStateStr(){
    switch (state) {
    case SAVED:
        return "已保存";
    case WAITFORSALEOUT:
        return "待销售";
    case WAITFORPURCHASEIN:
        return "待采购";
    case WAITFORSALEBACK:
        return "待采退";
    case FINISHED:
        return "已完成";
    }

    return "";
}

QString Dingdan::getTypeStr(){
    switch (type) {
    case SaleOut:
        return "销售单";
    case SaleBack:
        return "销退单";
    case PurchaseIn:
        return "采购单";
    case PurchaseBack:
        return "采退单";
    case PurchaseDingdan:
        return "采购订单";
    case TicketAdjust_Client:
    case TicketAdjust_Giver:
        return "票款调整";
    }

    return "";
}

void Dingdan::calculateTotal(){
    totalCount = 0;
    totalMoney = 0;
    ticketIn = 0;
    ticketOut = 0;
    moneyIn = 0;
    moneyOut = 0;
    profit = 0;
    for(DingdanDetail *detail:details){
        totalCount += detail->count;
        totalMoney += detail->count * detail->price;
        ticketIn += detail->ticketIn;
        ticketOut += detail->ticketOut;
        moneyIn += detail->moneyIn;
        moneyOut += detail->moneyOut;
        profit += detail->profit;
    }
}

UserRelation* Dingdan::getUserRelation(){
    DbHelper helper;
    if(type == SaleOut || type == SaleBack){
        relation = helper.getUserRelation(sourceUsername, targetUsername, UserRelation::Client);
    }else{
        relation = helper.getUserRelation(sourceUsername, targetUsername, UserRelation::Giver);
    }

    return relation;
}

UserRelation* Dingdan::getReverseRelation(){
    if(reverseRelation == nullptr){
        DbHelper helper;
        if(type == SaleOut || type == SaleBack){
            reverseRelation = helper.getUserRelation(targetUsername, sourceUsername, UserRelation::Giver);
        }else{
            reverseRelation = helper.getUserRelation(targetUsername, sourceUsername, UserRelation::Client);
        }
    }

    return reverseRelation;
}

void Dingdan::copy(Dingdan &dingdan){
    id = dingdan.id;
    dingdanId = dingdan.dingdanId;
    relatedDingdanId = dingdan.relatedDingdanId;
    sourceUsername = dingdan.sourceUsername;
    sourceCompanyId = dingdan.sourceCompanyId;
    targetUsername = dingdan.targetUsername;
    targetCompanyId = dingdan.targetCompanyId;
    state = dingdan.state;
    type = dingdan.type;
    date = dingdan.date;
    totalCount = dingdan.totalCount;
    totalMoney = dingdan.totalMoney;
    ticketIn = dingdan.ticketIn;
    ticketOut = dingdan.ticketOut;
    moneyIn = dingdan.moneyIn;
    moneyOut = dingdan.moneyOut;
    profit = dingdan.profit;
    remark = dingdan.remark;
    opNickName = dingdan.opNickName;

    sourceFreightPaid = dingdan.sourceFreightPaid;
    sourceFreightUnpaid = dingdan.sourceFreightUnpaid;
    targetFreightPaid = dingdan.targetFreightPaid;
    freeTicket = dingdan.freeTicket;
    freeMoney = dingdan.freeMoney;
    ticket = dingdan.ticket;
    money = dingdan.money;

    details = dingdan.details;
}

void Dingdan::clear(){
    id = 0;
    dingdanId.clear();
    sourceUsername.clear();
    sourceCompanyId.clear();
    targetUsername.clear();
    targetCompanyId.clear();
    state = Dingdan::SAVED;
    date.clear();
    totalCount = 0;
    totalMoney = 0;
    ticketIn = 0;
    ticketOut = 0;
    moneyIn = 0;
    moneyOut = 0;
    profit = 0;
    remark.clear();
    opNickName.clear();
}
